import { Middleware } from 'koa'
import Koa from 'koa'
import Router from 'koa-router'
import { MarkApi } from '../controller'
import dayjs, { Dayjs } from 'dayjs'

export async function createView(config: any): Promise<Middleware> {
    const router = new Router()

    const { host } = config
    const mark = new MarkApi(host, '/api/v3')


    router.get('/', async (ctx: Koa.ParameterizedContext<Koa.DefaultState, Koa.DefaultContext, any>, next) => {
        const data:string[] = []
        const priceData = await mark.price()
        for (const item of priceData) {
            data.push(item.symbol)
        }
        ctx.set('Content-Type','application/json')
        await ctx.render('index',{pair:data})
    })
    router.get('/index2', async (ctx: Koa.ParameterizedContext<Koa.DefaultState, Koa.DefaultContext, any>, next) => {
        const data:string[] = []
        const priceData = await mark.price()
        for (const item of priceData) {
            data.push(item.symbol)
        }
        dayjs().format("YYYY-MM-DD")
        ctx.set('Content-Type','application/json')
        await ctx.render('index2',{pair:data})
    })

    router.get('/getAllCoin',async (ctx,next)=>{
        const coins :string[] = []
        const priceData = await mark.price()
        for (const item of priceData) {
            coins.push(item.symbol)
        }
        ctx.set('Content-Type','application/json')
        ctx.body = coins
    })


    router.get('/getKlinesTime',async (ctx,next)=>{
        const arr = []
        const query:any = ctx.query
        const times = await mark.kinesTime(query)
        ctx.set('Content-type','application/json')
        ctx.body = times
    })


    router.get('/klines', async (ctx: Koa.ParameterizedContext<Koa.DefaultState, Koa.DefaultContext, any>, next) => {
        const query: any = ctx.query
        let klines = await mark.klinesByTime(query)
        ctx.set('Content-Type', 'application/json')
        ctx.body = klines
    })

    const ret = router.routes()
    return ret as Middleware
}